package 尊享面试.矩阵;

import java.util.Arrays;
import java.util.List;

public class a422有效的单词方块 {
    static class Solution {
        public boolean validWordSquare(List<String> words) {
            int n = words.size();
            for (int i = 0; i < n; i++) {
                if (words.get(i).length() > n) {
                    return false;
                }
                for (int j = i + 1; j < n; j++) {
                    char c1 = getChar(words, i, j);
                    char c2 = getChar(words, j, i);
                    if (c1 != c2) {
                        return false;
                    }
                }
            }
            return true;
        }

        private char getChar(List<String> words, int i, int j) {
            String word = words.get(i);
            return j < word.length() ? word.charAt(j) : ' ';
        }
    }

    public static void main(String[] args) {
        List<String> words = Arrays.asList("abc", "bde", "cefg");
        Solution solution = new Solution();
        System.out.println(solution.validWordSquare(words));
    }
}
